package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLObjectDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.mall.lib.domain.Mall;
import com.rockcent.mall.lib.domain.Merchant;
import com.rockcent.mall.lib.dto.MallDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by oyhk on 14-6-22.
 */
@Repository
public class MallRepositoryImpl implements MallRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MallRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<MallDto> findMallList(Long userId, PageResult pageResult) {
        SQLPageDoing<MallDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall m left join mall_certify_info mc on m.id = mc.mall_id where m.audit_certify_info_status = 'AUDIT_PASS' and m.id not in (select mall_id from merchant me where me.account_id = :userId)");
            sql.append(" and exists(select mci.mall_id from mall_certify_info mci where m.id = mci.mall_id)");
            params.put("userId", userId);
            fields.append("m.hostname hostName, m.customer_platform_domain customerPlatformDomain, m.id, m.name, m.serve_rate serveRate, m.qrcode_url qRCodeUrl, m.logo_small_picture logoSmallPicture, m.introduce, mc.industry");
        };
        return doing.go(pageResult, MallDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public MallDto findMallInfo(Long mallId) {
        SQLObjectDoing<MallDto> doing = (sql, fields, params, map) -> {
            sql.append("select %s from mall m left join mall_certify_info mc on m.id = mc.mall_id where m.audit_validate = 'YES' and m.id = :mallId");
            params.put("mallId", mallId);
            fields.append("m.id, m.name, m.serve_rate serveRate, m.qrcode_url qRCodeUrl, m.logo_small_picture logoSmallPicture, m.introduce, mc.industry");
        };
        return doing.go(MallDto.class, jdbcTemplate, log, objectMapper);
    }

    @Override
    public List<Mall> findByIdAndCertify(List<Long> idList) {
        SQLListDoing<Mall> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("select %s from mall m where m.id in (:idList) and m.audit_validate = 'YES'");
            sql.append(" and exists(select mc.mall_id from mall_certify_info mc where m.id = mc.mall_id)");
            params.put("idList", idList);
            fields.append("m.*");
        };
        return doing.go(Mall.class, jdbcTemplate, log, objectMapper);
    }

    /**
     * 通过域名查询平台信息
     * @param host
     * @return
     */
    @Override
    public Mall findByHostnameOrCustomerPlatformDomain(String host) {
        SQLObjectDoing<Mall> doing = (sql, fields, params, map) -> {
            sql.append("select %s from mall m where m.customer_platform_domain=:host or m.hostname=:host");
            params.put("host",host);
            fields.append("m.id,m.account_id as userInfoId,m.hostname,m.customer_business_domain," +
                    "m.customer_platform_domain,m.background_picture, m.logo_small_picture, " +
                    "m.logo_big_picture,m.icp_number,m.online_qq,m.service_telephone,m.name,m.tag,m.introduce");
        };
        return doing.go(Mall.class, jdbcTemplate, log, objectMapper);
    }

    /**
     * 查找平台下所有的认证通过的商户
     * @param mallId
     * @return
     */
    @Override
    public List<Merchant> findMerchantsByMallId(Long mallId) {
        SQLListDoing<Merchant> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM merchant m where 1=1");
            sql.append(" and is_delete='NO'");
            sql.append(" and m.status='VERIFIED'");
            sql.append(" and m.mall_id=:mallId");
            sql.append(" and not exists(select 1 from mall ma where ma.id=:mallId and ma.account_id=m.account_id)");
            params.put("mallId", mallId);
            fields.append("m.*");
        };
        return doing.go(Merchant.class, jdbcTemplate, log, objectMapper);
    }
}
